
// 'use strict';

// const Controller = require('egg').Controller;
// //发送支付功能
// const AlipaySDK = require('alipay-sdk').default;
// //将支付信息 formData 包裹属性
// const AlipayFormData = require('alipay-sdk/lib/form').default;

// class PayController extends Controller {
//     async alipay() {
//         const { ctx, config } = this;
//         const alipaySdk = new AlipaySDK({ //实例化
//             appId: '2021000118644708', //用户id
//             signType: 'RSA2',//秘钥格式
//             alipayPublicKey: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr48zf4UmuGhhqnQMmpw0T1bZ4esryqGzGjJ/40V36VqlAuFdcI6Z+qQvZKy2gFCDQMxNVdp4EJwUVSi5fMhISOj7XLgAP5Zi338QaYwasHyrPnW0wRH4PVrDGtMGCzFY9WiOIup1GGbNwP7N5coqsj0FRGOxJDPYW2RiP/4AGKCHXFEAtxNiuPXvmb95gdn3Gt6MUljC9AD5UK6u0+bogIuYnZFoQ6j/5iZPkHx4QgxvVFZYOmBMSaFMsWZioOfK4wFkKYhZL7iFDUdUv65ehg8DOR4JBKbZZvp6DP28JwtxTGASPU2D5WeXxb64OzpqZ0QkQd3EmXs73hcXj3RZHwIDAQAB',
//             privateKey: "MIIEpAIBAAKCAQEAinDYs6JpGh5relb6kUix1Ydsv69Nb10Hr4Wev4Cgy5bL6zs5CwMEdnyZ2Iygqbc0VjJXU25bKjW0ZpfhgqcBZIwcbnk+QMuj4tIwIFnDr9LmxTKRV3U/2vM70t8kvRjcimpIBkM592FGM7h4cZcW38bmKsddXwXLcLD7cYgpJyPjJbdFnaAE8IBwEvwLQz3GqDfs2YTT8xnzvLXY64hOnXZGSKOo1j3tJQoiffpo+1WUJdSU95VjRzYhW4DYiI2ImGPFnw1BSe/ptXjanasmehuD9ZoWJe0hTwMLc3ZFneVHysRyM4M2EtxqS+5U3MonpARcsrFE5gByQDoMD9+jWwIDAQABAoIBAALRZTfS2HzVna5x0sveRPBpv0bvlI3twNhLyf1v6tX6DdCN9g1JVG/s/VyxEBwkRIZ0o95o4qY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KC1RWhZnj6NFKXo7C3Wkoh6Ssri4bnVMsmewMT8Szq/g1bAcoBQFyYSZg==",
//             geteway: "https://openapi.alipaydev.com/gateway.do", //支付宝网关 
//             timeout: 5000,
//             camelcase: true, //网关返回的下划线 key改为驼峰
//         })
//         const formData = new AlipayFormData();

//         formData.setMethod('get')
//         formData.addField('appId', '2021000118644708')
//         formData.addField('charset', 'utf-8')
//         formData.addField('signType', 'RSA2')
//         formData.addField('bizContent', {
//             outTradeNo: '15693801274202321231', // 【必选】商户订单号：64个字符内，包含数字，字母，下划线；需要保证在商户端不重复
//             productCode: 'FAST_INSTANT_TRADE_PAY', // 【必选】销售产品码，目前仅支持FAST_INSTANT_TRADE_PAY
//             totalAmount: '1', // 【必选】订单总金额，精确到小数点后两位
//             subject: 'test', // 【必选】 订单标题
//             body: 'test' // 【可选】订单描述
//         })
//         console.log('formData:===============', formData)
//         /**
//          * exec对应参数：
//          * method（调用支付宝api）
//          * params（api请求的参数（包含“公共请求参数”和“业务参数”））
//          * options（validateSign，formData，log）
//          *
//          * 统一下单支付 exec
//          */
//         ctx.body = await alipaySdk.exec('alipay.trade.page.pay', {}, { formData })

//     }
// }

// module.exports = PayController;


/*
 * @Author: your name
 * @Date: 2021-10-27 10:18:45
 * @LastEditTime: 2021-10-27 17:02:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egg_ payment/app/controller/pay.js
 */
'use strict';

const Controller = require('egg').Controller;
// 发送支付功能
const AlipaySDK = require('alipay-sdk').default
// 将支付信息 formdata 包裹住
const AlipayFormData = require('alipay-sdk/lib/form').default

class PayController extends Controller {
    async alipay() {
        const { ctx, config } = this;
        const { allprice } = ctx.query
        const alipaySdk = new AlipaySDK({
            appId: '2021000118645842', // 你自己的沙箱环境的appId
            privateKey: "MIIEpAIBAAKCAQEAj/+1brlAKalMyytUchM8O3FiBbOqOAiqSDAQruc+aHsMKTDgzFxlk8BsZ8WfPybFs3WrEs8nQiAXDyLvAf0/OjcD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hnp4dBRs+vkwR14Y+EIpOZD5ImqhnHHJq9mVH+7TDgN/EA1IrUDN1is3HcJnIx/Su8CgYAYhkztY9rJbPQW3T5ARxQ1MVxEypUuXrWlkkDEKf71mjyyqxgnvQvHMKY/o/Bzq23lBmdJAAJD0c0hyMCiGXn+WwLok4IhJU3GB/cNnryhXwsoghEXsUXIia53cKD05WkbntyAYiTC4wRPUsb/WbPdJ3yE9fg6GZKhTYS6dssY4QKBgQCMHojY69uUlpTL7jHfoX40cHiSQY1euru1LCi7LwFxIm0EuqQOaUXHOM+qS0HVStWiVvO+G+znMKcWyChqQfW0YUkossQ7+mImfepZEfRG/K0jvQqrzG8NLb79qK5v6LT7ShgcJFJOltvHXXdXHY5sHoDyBC6QZEoKahf0oFuOgQ==", // 私钥
            signType: 'RSA2', // 签名类型
            alipayPublicKey: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwXElu8ykyOj6rbdfbOsaSw+doZKWJKxrny1VMtXO3ygLnIwVnOHdNv69vQyWhmihcv8IbH3Y9rM0uY42bCz2hKsLVxuW3R22qf375B3gDNemUyWL+73mGy9F+QZgskzaZJtq7vLdv32jNfCKU+0egqwJL7DzQ710b/mb2LFwjN9SXWckAZSv3f048TmpTVhGwm5TAf7hqc/mlSgyLFmgRzBJTRig/vUK9Jm+mmToBFJDsmAKGK13H6vFkbbN9sikFxcQj4xdfgO88Dz8c6HLoEYQB3vacpS8uAW0n8Ew3guTCzUUJ2iwUqKGGj5et2VNHloxY088/hZ8KVDuzxbS7wIDAQAB', // 支付宝公钥（不是应用公钥）
            gateway: 'https://openapi.alipaydev.com/gateway.do', // 网关地址
            timeout: 5000, // 网关超时时间
            camelcase: true // 是否把网关返回的下划线 key 转换为驼峰写法
        })

        const formData = new AlipayFormData();

        formData.setMethod('get'); //请求方式
        formData.addField('appId', '2021000118645842'); //用户id
        formData.addField('charset', 'utf-8'); //编码格式
        formData.addField('signType', 'RSA2');//注册信息格式
        formData.addField('bizContent', { //设置商户信息
            outTradeNo: new Date() * 1 + '', // 【必选】商户订单号：64个字符内，包含数字，字母，下划线；需要保证在商户端不重复
            productCode: 'FAST_INSTANT_TRADE_PAY', // 【必选】销售产品码(二维码)，目前仅支持FAST_INSTANT_TRADE_PAY
            totalAmount: allprice, // 【必选】订单总金额，精确到小数点后两位
            subject: '尊贵的凯迪拉克车主', // 【必选】 订单标题
            body: '愿七彩雨盆伴您一路前行' // 【可选】订单描述
        })
        console.log('formData:===============', formData)
        /**
         * exec对应参数：
         * method（调用支付宝api）
         * params（api请求的参数（包含“公共请求参数”和“业务参数”））
         * options（validateSign，formData，log）
         *
         * 统一下单支付 exec
         */
        ctx.body = await alipaySdk.exec('alipay.trade.page.pay', {}, { formData })
    }
}

module.exports = PayController;